package com.kfm.servlet.day04;

import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/verify")
public class VerifyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 获取验证码
        String verifyCode = req.getParameter("code");
        resp.setContentType("text/html;charset=utf-8");

        if (CaptchaUtil.ver(verifyCode, req)){
            resp.getWriter().write("验证码正确");
        } else{
            resp.getWriter().write("验证码错误");
        }
        CaptchaUtil.clear(req);

//        // 获取session中的验证码
//        String sessionVerifyCode = (String) req.getSession().getAttribute("captcha");
//
//        // 比较
//        if (verifyCode.equalsIgnoreCase(sessionVerifyCode)) {
//            resp.getWriter().write("验证码正确");
//        } else {
//            resp.getWriter().write("验证码错误");
//        }
    }
}
